package com.kt.thread.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by tangwy on 2016/3/23.
 */
public class ReentrantLockTest {
    private static final int SIZE=100;
    private int i=0;
    private final ReentrantLock lock = new ReentrantLock();
    private List<Integer> list=new ArrayList<Integer>(SIZE);
    public ReentrantLockTest(){
        for(int i=1;i<=SIZE;i++){
            list.add(i);
        }
    }

    public int getElement() {
       try {
           lock.lock();
           if(i<SIZE){
              return list.get(i++);
           }else {
               return -1;
           }
       }finally {
           lock.unlock();
       }
    }
}
